﻿using Microsoft.Office.Server.Search.Administration;
using Microsoft.Office.Server.Search.Query.Rules;
using SPSDeploy.Extensions.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Xml.Linq;

namespace SPSDeploy.Extensions.Extensions
{
    /// <summary>
    /// Query rules extensions
    /// </summary>
    internal static class QueryRulesExtension
    {

        /// <summary>
        /// Return query rule owners
        /// </summary>
        /// <param name="application"></param>
        /// <returns></returns>
        internal static List<SearchObjectOwner> GetOwners(SearchServiceApplication application)
        {
            SearchObjectOwner owner;
            List<SearchObjectOwner> result = new List<SearchObjectOwner>();
            using (SqlConnection cn = new SqlConnection(application.SearchAdminDatabase.DatabaseConnectionString))
            {
                cn.Open();
                SqlCommand cmd = new SqlCommand("SELECT DISTINCT o.FarmId, o.SiteCollectionId, o.SiteId FROM MSSQueryRules qr WITH (NOLOCK) JOIN MSSOwners o WITH (NOLOCK) ON o.Id = qr.OwnerId ORDER BY 1, 2, 3", cn) { CommandType = CommandType.Text };
                using (SqlDataReader r = cmd.ExecuteReader())
                    while (r.Read())
                        if ((owner = SearchObjectOwnerExtension.GetOwner(r)) != null)
                            result.Add(owner);
            }
            return result;
        }

        /// <summary>
        /// Return query rule owners
        /// </summary>
        /// <param name="proxy"></param>
        /// <returns></returns>
        internal static List<SearchObjectOwner> GetOwners(SearchServiceApplicationProxy proxy)
        {
            SearchObjectOwner owner;
            List<SearchObjectOwner> result = new List<SearchObjectOwner>();
            using (SqlConnection cn = new SqlConnection(ApplicationSection.Instance.SearchAdminDatabase.ConnectionString))
            {
                cn.Open();
                SqlCommand cmd = new SqlCommand("SELECT DISTINCT o.FarmId, o.SiteCollectionId, o.SiteId FROM MSSQueryRules qr WITH (NOLOCK) JOIN MSSOwners o WITH (NOLOCK) ON o.Id = qr.OwnerId WHERE o.FarmId IS NOT NULL ORDER BY 1, 2, 3", cn) { CommandType = CommandType.Text };
                using (SqlDataReader r = cmd.ExecuteReader())
                    while (r.Read())
                        if ((owner = SearchObjectOwnerExtension.GetOwner(r)) != null)
                            result.Add(owner);
            }
            return result;
        }

        /// <summary>
        /// Compare query rule instances
        /// </summary>
        /// <param name="element"></param>
        /// <param name="item"></param>
        /// <param name="owner"></param>
        /// <returns></returns>
        internal static bool Equals(XElement element, QueryRule item, SearchObjectOwner owner)
        {
            return element.Attribute("name").Value == item.DisplayName &&
                element.Attribute("level").Value == owner.Level.ToString() &&
                element.Attribute("siteId").Value == owner.SPSiteId.ToString() &&
                element.Attribute("webId").Value == owner.SPWebId.ToString();
        }

    }
}